package net.adst.ui.other;

import com.toedter.calendar.JDateChooser;
import net.adst.resources.Constants;
import net.adst.util.GBC;
import sun.net.ResourceManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 21.11.12
 * Time: 5:08
 * To change this template use File | Settings | File Templates.
 */
public class AddNaemItemDialog extends JDialog {
    private JLabel dateLabel;
    private JDateChooser dateChooser;

    private JLabel chINLabel;
    private JTextField chINField;
    private JButton backButton;
    private JButton nextButton;

    private JLabel chOutLabel;
    private JTextField chOutField;

    private JLabel tecNameLabel;
    private JComboBox tecNameCombo;

    private JLabel managerNameLabel;
    private JComboBox managerNameCombo;

    private JLabel customerLabel;
    private JComboBox customerCombo;

    private JLabel priceHoursCustLabel;
    private JTextField priceHoursCustField;

    private JLabel hoursCustLabel;
    private JTextField hoursCustField;

    private JLabel incomeLabel;
    private JTextField incomeField;

    private JLabel performerLabel;
    private JTextField performerField;

    private JLabel priceHoursOurLabel;
    private JTextField priceHoursOurField;

    private JLabel hoursOurLabel;
    private JTextField hoursOurField;

    private JLabel amountOurLabel;
    private JTextField amountOurField;

    private JLabel profitLabel;
    private JTextField profitField;

    private JLabel objectNameLabel;
    private JTextField objectNameField;

    private JLabel vozvratLabel;
    private JTextField vozvratField;

    private JLabel commentLabel;
    private JTextArea commentArea;

    private JButton addButton;
    private JButton cancelButton;

    public AddNaemItemDialog() {
        setLayout(new GridBagLayout());
        addElements();
        addLogic();
    }

    private void addElements() {
        ImageIcon backIcon = new ImageIcon(ResourceManager.class.getResource("/images/back.png"));
        ImageIcon nextIcon = new ImageIcon(ResourceManager.class.getResource("/images/next.png"));
        backButton = new JButton(backIcon);
        nextButton = new JButton(nextIcon);
        backButton.setMargin(new Insets(3, 3, 3, 3));
        backButton.setFocusPainted(false);
        nextButton.setMargin(new Insets(3, 3, 3, 3));
        nextButton.setFocusPainted(false);

        dateLabel = new JLabel("дата:");
        dateChooser = new JDateChooser();
        dateChooser.setMaximumSize(new Dimension(90, 18));
        dateChooser.setPreferredSize(new Dimension(90, 18));
        dateChooser.setMinimumSize(new Dimension(90, 18));
        dateChooser.setCalendar(Calendar.getInstance());

        Dimension labelDimension = new Dimension(110, 20);
        Dimension fieldDimension = new Dimension(100, 20);
        chINLabel = new JLabel("счет входящий:");
        chINLabel.setMaximumSize(labelDimension);
        chINLabel.setPreferredSize(labelDimension);
        chINLabel.setMinimumSize(labelDimension);
        chINField = new JTextField();
        chINField.setMaximumSize(fieldDimension);
        chINField.setPreferredSize(fieldDimension);
        chINField.setMinimumSize(fieldDimension);

        chOutLabel = new JLabel("счет исходящий:");
        chOutLabel.setMaximumSize(labelDimension);
        chOutLabel.setPreferredSize(labelDimension);
        chOutLabel.setMinimumSize(labelDimension);
        chOutField = new JTextField();
        chOutField.setMaximumSize(fieldDimension);
        chOutField.setPreferredSize(fieldDimension);
        chOutField.setMinimumSize(fieldDimension);

        tecNameLabel = new JLabel("именование техники:");
        tecNameLabel.setMaximumSize(labelDimension);
        tecNameLabel.setPreferredSize(labelDimension);
        tecNameLabel.setMinimumSize(labelDimension);
        tecNameCombo = new JComboBox();
        tecNameCombo.setEditable(true);
        tecNameCombo.setMaximumSize(fieldDimension);
        tecNameCombo.setPreferredSize(fieldDimension);
        tecNameCombo.setMinimumSize(fieldDimension);

        managerNameLabel = new JLabel("менагер:");
        managerNameLabel.setMaximumSize(labelDimension);
        managerNameLabel.setPreferredSize(labelDimension);
        managerNameLabel.setMinimumSize(labelDimension);
        managerNameCombo = new JComboBox();
        managerNameCombo.setEditable(true);
        managerNameCombo.setMaximumSize(fieldDimension);
        managerNameCombo.setPreferredSize(fieldDimension);
        managerNameCombo.setMinimumSize(fieldDimension);

        customerLabel = new JLabel("заказчик:");
        customerLabel.setMaximumSize(labelDimension);
        customerLabel.setPreferredSize(labelDimension);
        customerLabel.setMinimumSize(labelDimension);
        customerCombo = new JComboBox();
        customerCombo.setEditable(true);
        customerCombo.setMaximumSize(fieldDimension);
        customerCombo.setPreferredSize(fieldDimension);
        customerCombo.setMinimumSize(fieldDimension);

        priceHoursCustLabel = new JLabel("почем(час):");
        priceHoursCustLabel.setMaximumSize(labelDimension);
        priceHoursCustLabel.setPreferredSize(labelDimension);
        priceHoursCustLabel.setMinimumSize(labelDimension);
        priceHoursCustField = new JTextField();
        priceHoursCustField.setMaximumSize(fieldDimension);
        priceHoursCustField.setPreferredSize(fieldDimension);
        priceHoursCustField.setMinimumSize(fieldDimension);

        hoursCustLabel = new JLabel("кол-во часов:");
        hoursCustLabel.setMaximumSize(labelDimension);
        hoursCustLabel.setPreferredSize(labelDimension);
        hoursCustLabel.setMinimumSize(labelDimension);
        hoursCustField = new JTextField();
        hoursCustField.setMaximumSize(fieldDimension);
        hoursCustField.setPreferredSize(fieldDimension);
        hoursCustField.setMinimumSize(fieldDimension);

        incomeLabel = new JLabel("доход:");
        incomeLabel.setMaximumSize(labelDimension);
        incomeLabel.setPreferredSize(labelDimension);
        incomeLabel.setMinimumSize(labelDimension);
        incomeField = new JTextField();
        incomeField.setMaximumSize(fieldDimension);
        incomeField.setPreferredSize(fieldDimension);
        incomeField.setMinimumSize(fieldDimension);

        performerLabel = new JLabel("исполнитель:");
        performerLabel.setMaximumSize(labelDimension);
        performerLabel.setPreferredSize(labelDimension);
        performerLabel.setMinimumSize(labelDimension);
        performerField = new JTextField();
        performerField.setMaximumSize(fieldDimension);
        performerField.setPreferredSize(fieldDimension);
        performerField.setMinimumSize(fieldDimension);

        priceHoursOurLabel = new JLabel("почем(час):");
        priceHoursOurLabel.setMaximumSize(labelDimension);
        priceHoursOurLabel.setPreferredSize(labelDimension);
        priceHoursOurLabel.setMinimumSize(labelDimension);
        priceHoursOurField = new JTextField();
        priceHoursOurField.setMaximumSize(fieldDimension);
        priceHoursOurField.setPreferredSize(fieldDimension);
        priceHoursOurField.setMinimumSize(fieldDimension);

        hoursOurLabel = new JLabel("кол-во часов:");
        hoursOurLabel.setMaximumSize(labelDimension);
        hoursOurLabel.setPreferredSize(labelDimension);
        hoursOurLabel.setMinimumSize(labelDimension);
        hoursOurField = new JTextField();
        hoursOurField.setMaximumSize(fieldDimension);
        hoursOurField.setPreferredSize(fieldDimension);
        hoursOurField.setMinimumSize(fieldDimension);

        amountOurLabel = new JLabel("расход:");
        amountOurLabel.setMaximumSize(labelDimension);
        amountOurLabel.setPreferredSize(labelDimension);
        amountOurLabel.setMinimumSize(labelDimension);
        amountOurField = new JTextField();
        amountOurField.setMaximumSize(fieldDimension);
        amountOurField.setPreferredSize(fieldDimension);
        amountOurField.setMinimumSize(fieldDimension);

        profitLabel = new JLabel("прибыль:");
        profitLabel.setMaximumSize(labelDimension);
        profitLabel.setPreferredSize(labelDimension);
        profitLabel.setMinimumSize(labelDimension);
        profitField = new JTextField();
        profitField.setMaximumSize(fieldDimension);
        profitField.setPreferredSize(fieldDimension);
        profitField.setMinimumSize(fieldDimension);

        objectNameLabel = new JLabel("объект:");
        objectNameLabel.setMaximumSize(labelDimension);
        objectNameLabel.setPreferredSize(labelDimension);
        objectNameLabel.setMinimumSize(labelDimension);
        objectNameField = new JTextField();
        objectNameField.setMaximumSize(fieldDimension);
        objectNameField.setPreferredSize(fieldDimension);
        objectNameField.setMinimumSize(fieldDimension);

        vozvratLabel = new JLabel("возврат:");
        vozvratLabel.setMaximumSize(labelDimension);
        vozvratLabel.setPreferredSize(labelDimension);
        vozvratLabel.setMinimumSize(labelDimension);
        vozvratField = new JTextField();
        vozvratField.setMaximumSize(fieldDimension);
        vozvratField.setPreferredSize(fieldDimension);
        vozvratField.setMinimumSize(fieldDimension);

        commentLabel = new JLabel("пометка:");
        commentLabel.setMaximumSize(labelDimension);
        commentLabel.setPreferredSize(labelDimension);
        commentLabel.setMinimumSize(labelDimension);
        commentArea = new JTextArea();
        commentArea.setLineWrap(true);
        commentArea.setFont(UIManager.getDefaults().getFont("TabbedPane.font"));
        JScrollPane commentScrollPane = new JScrollPane(commentArea);
        commentScrollPane.setMaximumSize(new Dimension(130, 55));
        commentScrollPane.setPreferredSize(new Dimension(130, 55));
        commentScrollPane.setMinimumSize(new Dimension(130, 55));


        addButton = new JButton("Добавить");
        cancelButton = new JButton("Отменить");

        add(dateLabel, new GBC(0, 0).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(backButton, new GBC(1, 0).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(dateChooser, new GBC(2, 0).setInsets(5, 0).setAnchor(GBC.NORTHWEST).setIpad(15, 0));
        add(nextButton, new GBC(3, 0).setInsets(5, 0).setAnchor(GBC.NORTHWEST));
        add(chINLabel, new GBC(0, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(chINField, new GBC(1, 1, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(chOutLabel, new GBC(0, 2).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(chOutField, new GBC(1, 2, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(tecNameLabel, new GBC(0, 3).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(tecNameCombo, new GBC(1, 3, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(managerNameLabel, new GBC(0, 4).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(managerNameCombo, new GBC(1, 4, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(customerLabel, new GBC(0, 5).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(customerCombo, new GBC(1, 5, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(priceHoursCustLabel, new GBC(0, 6).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(priceHoursCustField, new GBC(1, 6, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursCustLabel, new GBC(0, 7).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursCustField, new GBC(1, 7, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(incomeLabel, new GBC(0, 8).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(incomeField, new GBC(1, 8, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(performerLabel, new GBC(0, 9).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(performerField, new GBC(1, 9, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(priceHoursOurLabel, new GBC(0, 10).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(priceHoursOurField, new GBC(1, 10, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursOurLabel, new GBC(0, 11).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(hoursOurField, new GBC(1, 11, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(amountOurLabel, new GBC(0, 12).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(amountOurField, new GBC(1, 12, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(profitLabel, new GBC(0, 13).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(profitField, new GBC(1, 13, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(objectNameLabel, new GBC(0, 14).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(objectNameField, new GBC(1, 14, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(vozvratLabel, new GBC(0, 15).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(vozvratField, new GBC(1, 15, 3, 1).setInsets(5, 5).setAnchor(GBC.NORTHWEST));

        add(commentLabel, new GBC(0, 16).setInsets(5, 5).setAnchor(GBC.NORTHWEST));
        add(commentScrollPane, new GBC(1, 16, 3, 1).setInsets(5, 5, 11, 5).setWeight(1, 1).setFill(GBC.BOTH));

        add(addButton, new GBC(0, 17).setInsets(5, 5).setAnchor(GBC.CENTER));
        add(cancelButton, new GBC(1, 17, 3, 1).setInsets(5, 5).setAnchor(GBC.CENTER));
    }

    public void init() {
        setDialogSettings();
        getContentPane().setBackground(Constants.COLOR_LIGHT_BLUE);
//        fillTextFields();
        setVisible(true);
    }

    private void setDialogSettings() {
//        setResizable(false);
        setModal(true);
        setModalityType(ModalityType.APPLICATION_MODAL);
        pack();
        setLocationRelativeTo(Frame.getFrames()[0]);
        setTitle("Добавление записи в таблицу \"Наем. тех.\"");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    }

    private void addLogic() {
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //ORS-65 fix
                if ((dateChooser.getDate() == null)) {
                    return;
                }
                Calendar dtFr = dateChooser.getCalendar();
                dtFr.add(Calendar.DATE, 1);
                dateChooser.setCalendar(dtFr);
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //ORS-65 fix
                if ((dateChooser.getDate() == null)) {
                    return;
                }
                Calendar dtFr = dateChooser.getCalendar();
                dtFr.add(Calendar.DATE, -1);
                dateChooser.setCalendar(dtFr);
            }
        });

    }

}
